package com.tensquare.article.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.article.pojo.Article;
import com.tensquare.article.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("article")
@CrossOrigin
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @RequestMapping(value = "{articleId}",method = RequestMethod.GET)
    public Result findById(@PathVariable String articleId){
        Article article = articleService.findById(articleId);
        return new Result(true, StatusCode.OK,"查询成功",article);
    }

    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){
        List<Article> list = articleService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",list);
    }
    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Article article){
        articleService.sava(article);
        return new Result(true,StatusCode.OK,"新增成功");
    }
    @RequestMapping(value = "{articleId}",method = RequestMethod.POST)
    public Result updateById(@PathVariable String articleId,
                             @RequestBody Article article){
        //设置Id
        article.setId(articleId);
        //执行修改
        articleService.updateById(article);
        return new Result(true,StatusCode.OK,"修改成功");

    }
    @RequestMapping(value = "{articleId}",method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable String articleId){
        articleService.deleteById(articleId);
        return new Result(true,StatusCode.OK,"删除成功");
    }
    @RequestMapping(value = "search/{page}/{size}",method = RequestMethod.POST)
    public Result findByPage(@PathVariable Integer page,
                             @PathVariable Integer size,
                             @RequestBody Map<String,Object> map){
       Page<Article> pageData =  articleService.findByPage(map,page,size);
        PageResult<Article> pageResult = new PageResult<>(
                pageData.getTotal(),pageData.getRecords()
        );
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

}
